﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FileCopyer
{
    internal class JSImportUpdater2
    {
        public static void UpdateImporter(string cesiumFileName,string bariumFileName)
        {
            if (File.Exists(cesiumFileName) == false) return;
            var lines = File.ReadAllLines(cesiumFileName);

            bool isStarted = false;
            for (var i = 0; i < lines.Length; i++)
            {
                if (IsImportFormat(lines[i],out string selFileName))
                {
                    var oldRefFileName= CombinePath(cesiumFileName,Regex.Replace(selFileName, "/","\\"));
                    var newRefFileName = oldRefFileName.Substring(oldRefFileName.IndexOf("packages\\engine"));
                    newRefFileName=Path.Combine(GlobalConfig.Instance.BariumFileName,newRefFileName);
                    if (File.Exists(newRefFileName))
                    {
                        var selFileName2 = GetRelativePath(newRefFileName, bariumFileName);
                        selFileName = "\"" + selFileName2 + "\";";
                        var l4 = lines[i].Split(' ');
                        lines[i] = l4[0] + " " + l4[1] + " " + l4[2] + " " + selFileName;
                    }
                    isStarted = true;
                }
                else if (isStarted)
                {
                    break;
                }
            }

            File.WriteAllLines(bariumFileName, lines);
        }
        private static string CombinePath(string cesiumFileName, string separator)
        {
            var path1= Path.GetDirectoryName(cesiumFileName).TrimEnd('\\');
            while (separator.StartsWith("..\\"))
            {
                path1 = path1.Substring(0, path1.LastIndexOf("\\"));
                separator = separator.Substring(3);
            }
            if(separator.StartsWith(".\\"))
            {
                separator = separator.Substring(2);
            }
            return Path.Combine(path1, separator);
        }

        private static string GetRelativePath(string newRefFileName, string bariumFileName)
        {
            var path1=Path.GetDirectoryName(newRefFileName);
            var path2=Path.GetDirectoryName(bariumFileName);
            for(var i=0;i<1000;i++)
            {
                if (path1[i] == path2[i])
                {
                    continue;
                }
                path1 = path1.Substring(i);
                path2 = path2.Substring(i);
                break;
            }
            var sp = path2.Split('\\');
            var res = "";
            for(var i = 0; i < sp.Length; i++)
            {
                res += "../";
            }
            res = "./" + res+ path1 + "/" + Path.GetFileName(newRefFileName);
            return res;
        }

        private static bool IsImportFormat(string line,out string selFileName)
        {
            line = line.Trim();
            var l4 = line.Split(' ');
            selFileName = null;
            if (l4.Length != 4) return false;
            if (l4[0] != "import" || l4[2] != "from") return false;
            if (l4[3].StartsWith( "\"")==false) return false;
            selFileName = l4[3].TrimEnd(';').Trim('"').Trim();
            return true;
        }
    }
}
